#include <assert.h>
#include <stdio.h>
#include "common.h"
#include "track.h"
#include "train.h"
#include "route.h"
void test_speed_limit_valid(Locomotive *loco)
{
    double maxspeed = ElectricMotorProperty_getMaxPerimeterSpeed(&(((ElectricLocomotive *)loco)->motorProperty), loco->wheelDiameter);
    ASSERT_FLOAT_CLOSE(maxspeed, 129.3234996); // PI * 1.25 * 2662 / 60 / 4.85 * 3.6
}

void test_motor_torque_limit(Locomotive *loco)
{
}

int main()
{

    Track *track0 = Track_create(0);
    Track *track1 = Track_create(1);
    ElectricMotorProperty motorProperty = ElectricMotorProperty_createValue(
        "YJ85-A",
        THREE_PHASED_ASYNC,
        1250,
        11852,
        2662,
        0.92,
        4.85);
    ElectricMotor *motors = ElectricMotor_createMulti(6);

    ElectricLocomotive *loco = Locomotive_create_electric(10, 23, 23.0 * 6, 1.25, 6, motorProperty);
    printf("motor maxpower %lf\n", motorProperty.maxPower);
    printf("motor maxpower %lf\n", loco->motorProperty.maxPower);
    assert(FLOAT_CLOSE(loco->base.startSpeed, 10.0));
    assert(loco->base.maxPower > 1000.0);
    ASSERT_FLOAT_CLOSE(loco->motorProperty.gearRatio, 4.85);
    ASSERT_FLOAT_CLOSE(loco->motorProperty.maxRPM, 2662.0);
    test_speed_limit_valid((Locomotive *)loco);
    const int WAGON_NUM = 2;
    RailroadCarProperty *property = RailroadCarProperty_create("N6", RAILROAD_CAR_TYPE_FLATCAR, 12.5, 100, 21.5, 60);
    RailroadWagon **wagons = malloc(sizeof(RailroadWagon *) * WAGON_NUM);
    for (int i = 0; i < 2; i++)
    {
        wagons[i] = RailroadWagon_create(property);
    }

    Train *train = Train_create(((Locomotive **)&loco), 1, wagons, WAGON_NUM);
    
    // 创建火车的路线。
    // 分配内存用于存储一个 Track 指针数组，数组大小为1
    // 这里假设 routeTracks 将用于存储构成路线的轨道（Track）指针
    Track **routeTracks = malloc(sizeof(Track *) * 2);
    routeTracks[0] = track0;
    routeTracks[1] = track1;
    printf("routeTracks %p\n", routeTracks[0]);
    DIRECTION* directions = malloc(sizeof(DIRECTION) * 1);
    directions[0] = DIRECTION_DOWN;
    Route *route = Route_create(routeTracks, directions, 1);
    printf("route length %d\n", route->length);
    Train_setRoute(train, route);
    
    // 设置火车为加速状态（这里需要改一下！）
    // 
    train->trainDynamics.state = TRAIN_STATE_ACCELERATING;
    Track_addTrain(track0, train);

    for (int i = 0; i < 100; i++)
    {
        // 后面应该添加一个world之类的对象，里面用来管理所有的火车。
        Track_update(track0);
        // if (train->trainDynamics.speed > 80)
        // {
        //     break;
        // }
        printf("%lf\n", train->trainDynamics.speed);
    }
}